//
//  Logger.cpp
//  Prism
//
//  Created by Bas du Pre on 24-05-11.
//  Copyright 2011 UU. All rights reserved.
//

#include <iostream>
#include <ctime>

#include "Logger.h"

using namespace Prism;

Logger *Logger::s_instance = 0;

Logger *Logger::instance() {
    if (!s_instance) s_instance = new Logger;
    return s_instance;
}

Logger::Logger() {
    
}

Logger::~Logger() {
    
}

void printTime() {
    time_t rawtime;
    struct tm * timeinfo;
    time(&rawtime);
    timeinfo = localtime(&rawtime);
    std::cout << timeinfo->tm_mday << "-" << (timeinfo->tm_mon + 1) << "-" << 1900 + timeinfo->tm_year << " " << timeinfo->tm_hour << ":" << timeinfo->tm_min << ":" << timeinfo->tm_sec << " ";
    
}

void Logger::log(String format, ...) {
    va_list ap;
    va_start(ap, format); //Requires the last fixed parameter (to get the address)
    
    printTime();
    std::cout << ": ";
    String msg;
    msg += String::fromFormatAP(format, ap);
        
    std::cout << msg.c_str();
    
    va_end(ap);
    
    std::cout << std::endl;
    
    _lastMessage = msg;
}

void Logger::logInternal(String format, const char * function, const char * file, int line, ...) {
    va_list ap;
    va_start(ap, line); //Requires the last fixed parameter (to get the address)
    
    printTime();
    String filename = file;
    if (filename.find_last_of("/\\") != -1) {
        filename = filename.substr(filename.find_last_of("/\\") + 1);
    }
    std::cout << filename.c_str() << ":" << line << " (" << function << ") ";
    std::cout << ": ";
    String msg;
    msg += String::fromFormatAP(format, ap);
    
    std::cout << msg.c_str();
    
    va_end(ap);
    
    std::cout << std::endl;
    
    _lastMessage = msg;
}

void Logger::logError(String format, ...) {    
    va_list ap;
    va_start(ap, format); //Requires the last fixed parameter (to get the address)
    
    printTime();
    String msg = "ERROR: ";
    msg += String::fromFormatAP(format, ap);
    std::cout << msg.c_str();
    
    va_end(ap);
    
    std::cout << std::endl;
    
    _lastMessage = msg;
}

void Logger::logErrorInternal(String format, const char * function, const char * file, int line, ...) {
    va_list ap;
    va_start(ap, line); //Requires the last fixed parameter (to get the address)
    
    printTime();
    String filename = file;
    if (filename.find_last_of("/\\") != -1) {
        filename = filename.substr(filename.find_last_of("/\\") + 1);
    }
    std::cout << filename.c_str() << ":" << line << " (" << function << ") ";
    String msg = "ERROR: ";
    msg += String::fromFormatAP(format, ap);
    std::cout << msg.c_str();
    
    va_end(ap);
    
    std::cout << std::endl;
    
    _lastMessage = msg;
}

void Logger::logInfo(String format, ...) {
    va_list ap;
    va_start(ap, format); //Requires the last fixed parameter (to get the address)
    
    printTime();
    String msg = "info: ";
    msg += String::fromFormatAP(format, ap);
    std::cout << msg.c_str();
    
    va_end(ap);
    
    std::cout << std::endl;
    
    _lastMessage = msg;
}

void Logger::logInfoInternal(String format, const char * function, const char * file, int line, ...) {
    va_list ap;
    va_start(ap, line); //Requires the last fixed parameter (to get the address)
    
    printTime();
    String filename = file;
    if (filename.find_last_of("/\\") != -1) {
        filename = filename.substr(filename.find_last_of("/\\") + 1);
    }
    std::cout << filename.c_str() << ":" << line << " (" << function << ") ";
    String msg = "info: ";
    msg += String::fromFormatAP(format, ap);
    std::cout << msg.c_str();
    
    va_end(ap);
    
    std::cout << std::endl;
    
    _lastMessage = msg;
}

String Logger::lastMessage() {
    return _lastMessage;
}
